/**
 * @author Administrator
 * 用户选择顺序列表
 */
var selectuserorderwin,selectuserorderidtager='',userordersm,orderoldnode=0, grid='',gird2='',
selectuserordernametager='',selectorderusersort='',selectorderusersort2= new Ext.data.Store(),
orderuserid=new Ext.data.Store();
function initselectuserorder(){
		 Ext.BLANK_IMAGE_URL=Ext.get('basePath').getValue()+"ajaxFramework/ext-2.1/resources/images/default/s.gif";

	 // example of custom renderer function
    function change(val){
        if(val > 0){
            return '<span style="color:green;">' + val + '</span>';
        }else if(val < 0){
            return '<span style="color:red;">' + val + '</span>';
        }
        return val;
    }

    // example of custom renderer function
    function pctChange(val){
        if(val > 0){
            return '<span style="color:green;">' + val + '%</span>';
        }else if(val < 0){
            return '<span style="color:red;">' + val + '%</span>';
        }
        return val;
    }
	 var url = Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/departmenttree.do';
	 var load = new Ext.tree.TreeLoader({
	 	dataUrl:url
	 });
	 var root = new Ext.tree.AsyncTreeNode({
	 	id:'0',
	 	text:'天津利达粮油有限公司'
	 })
	 var tree = new Ext.tree.TreePanel({
	 		x:0,
	 		y:0,
        	autoScroll: true,
        	animate: true,
        	containerScroll: true,
        	border: true,
	        loader:load,
	        height:500,
	        width:230,
	        anchor:'0% 100%'
	    });
	tree.setRootNode(root); 
	tree.on('click',function(node){
		selectorderusersort.load({
			params:{departmentid:node.id}
		});
	})
	/**
	 * 用户列表
	 */
	var userlisturl = Ext.get('basePath').getValue()+'tjsinfo/tjsoa/system/selectUserByDepartmentList.do'
	selectorderusersort = new Ext.data.JsonStore({
		url:userlisturl,
		fields:['userId','uuid','userName']
	});
	selectorderusersort2 = new Ext.data.JsonStore({
		url:userlisturl,
		fields:['userId','uuid','userName']
	});
	userordersm  = new Ext.grid.CheckboxSelectionModel({
		handleMouseDown:Ext.emptyFn
	});
	var cm = new Ext.grid.ColumnModel([
		userordersm,
		{header: "用户名", width: 75, dataIndex: 'userName',menuDisabled : true},
		{header: "userId", width: 75, dataIndex: 'userId',menuDisabled : true,hidden:true},
		{header: "uuid", width: 75, dataIndex: 'uuid',menuDisabled : true,hidden:true}
	]);
	var cm2 = new Ext.grid.ColumnModel([
		{header: "用户名", width: 75, dataIndex: 'userName',menuDisabled : true},
		{header: "操作", width: 75, dataIndex: 'operate',menuDisabled : true,renderer:function(s,m,rec){
		return "<a  href="+"javascript:clearorderuser('"+rec.data.userId+"')>移除</a>";
		}},
		{header: "userId", width: 75, dataIndex: 'userId',menuDisabled : true,hidden:true},
		{header: "uuid", width: 75, dataIndex: 'uuid',menuDisabled : true,hidden:true}
	]);
   grid = new Ext.grid.GridPanel({
		x:tree.width,
	 	y:0,
		cm:cm,
		sm:userordersm,
		width:300,
		height:430,
		anchor:'100% 100%',
		stripeRows: true,
		store:selectorderusersort,
		border: true,
	   	viewConfig:{
			forceFit:true	
		},
		tbar:[{
			 text: '右移',
             iconCls: 'silk-add',
             handler:function(){
						var orderuser  = userordersm.getSelections();
						for(var i=0;i<orderuser.length;i++)
						{
							var index = selectorderusersort2.findBy(function(record,id){
							 return	 record.data.userId ==  orderuser[i].data.userId
							});
							if(index == -1)
							{
								selectorderusersort2.addSorted(orderuser[i]);
							}
						}
					}
		}],
        buttonAlign:'right'
	});
	 grid2 = new Ext.grid.GridPanel({
		id:'grid2',
		x:tree.width+grid.width,
	 	y:0,
		cm:cm2,
		width:300,
		height:430,
		anchor:'100% 100%',
		stripeRows: true,
		enableDragDrop:true,
		dropConfig: {
            appendOnly:true
        },
        ddGroup: "GridDD2",
		store:selectorderusersort2,
		border: true,
	   	viewConfig:{
			forceFit:true	
		}
	});
	selectuserorderwin = new Ext.Window({
		layout:'absolute',
		title:'人员选择',
		width:820,
		height:500,
		modal:true,
		closeAction:'hide',
		items:[tree,grid,grid2],
		buttonAlign:'right',
		buttons:[{
			text:'保存',
			handler:function(){
				var userid='',username='',nodeid='';
				selectorderusersort2.each(function(record){
					userid+=record.data.userId+',';
					username+=record.data.userName+',';
				})
				$('#'+selectuserorderidtager).attr('value',userid);
				$('#'+selectuserordernametager).attr('value',username);
				 
				selectuserorderwin.hide();	
			}
		},{
			text:'取消',
			handler:function(){
				selectuserorderwin.hide();		
			}
		}]
	})
}

/**
 * 打开顺序人员树
 * @param {} id
 * @param {} name
 */
function selectuserorderwinshow(id,name){
	selectuserorderidtager = id;
	selectuserordernametager = name;
	var userid = $('#'+selectuserorderidtager).attr('value');
	var username = $('#'+selectuserordernametager).attr('value');
	if(userordersm.getCount() > 0)
	{
		userordersm.clearSelections();	
	}
	if(typeof(userid)   ==   'undefined')
	{
				
			selectorderusersort2.removeAll();
	}
	else
	{
			selectorderusersort2.removeAll();
			var useridarray = userid.split(",");
			var usernamearray = username.split(",");
			for(var i = 0;i<useridarray.length-1;i++)
			{
				selectorderusersort2.addSorted(new Ext.data.Record({
					userId:useridarray[i],
					userName:usernamearray[i]
				}));
			}
	}
	selectuserorderwin.show();
	selectuserorderwin.findByType('treepanel')[0].root.expand();  
	var ddrow = new Ext.dd.DropTarget(grid2.getEl(), {
        ddGroup: "GridDD2", // Data come from
        // copy:true,
        notifyDrop : function(dd, e, data){
            var rows=grid2.getSelectionModel().getSelections();
            var count = rows.length;
            var cindex=dd.getDragData(e).rowIndex;
            var array=[];
            for(var i=0;i<count;i++){
                var index = cindex+i;
                array.push(index);
            }
          selectorderusersort2.remove(selectorderusersort2.getById(data.selections[0].id));
            selectorderusersort2.insert(cindex,data.selections); 
            grid2.getView().refresh(); 
            grid2.getSelectionModel().selectRows(array); 
        }
    });
}
/**
 * 清楚选择的顺序用户
 * @param {} id
 */
function clearorderuser(id){
	var index = selectorderusersort2.find('userId',id);
	if (index != -1) {
		selectorderusersort2.remove(selectorderusersort2.getAt(index));	
	}
	
}